package com.jinxin.platform.cdqzsqacs.utils;

import org.apache.commons.lang3.StringUtils;
import org.apache.tomcat.util.codec.binary.Base64;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.security.Security;

/**
 * @author : zhangpeng
 * @date : 2021-05-13 15:45:22
 **/

@SuppressWarnings({"restriction"})
public class ThreeDES {
    private static Logger logger = LoggerFactory.getLogger(ThreeDES.class);

    /**
     * 算法名称
     */
    public static final String KEY_ALGORITHM = "DESede";
    /**
     * 算法名称/加密模式/填充方式
     */
    public static final String CIPHER_ALGORITHM = "DESede/ECB/PKCS7Padding";
    /**
     * 默认秘钥
     */
    public static final String DEFAULT_PASSWORD = "296CD6EB2CA94321ABEF575F";


    /**
     * 加密数据，使用默认秘钥
     *
     * @param data 加密的数据
     * @return
     */
    public static String encrypt(String data) {
        return encrypt(data, DEFAULT_PASSWORD);
    }

    /**
     * 解密数据，使用默认秘钥
     *
     * @param data 解密的数据
     * @return
     */
    public static String decrypt(String data) throws Exception {
        return decrypt(data, DEFAULT_PASSWORD);
    }

    /**
     * 加密数据 -3DES
     *
     * @param data     源数据
     * @param password 秘钥 16位或者24位
     * @return
     * @throws Exception
     */
    public static String encrypt(String data, String password) {
        if (StringUtils.isBlank(password)) {
            logger.error("3DES加密，秘钥为空");
            return null;
        }
        if (password.length() != 16 && password.length() != 24) {
            logger.error("3DES加密，秘钥长度错误，长度需要16位或者24位，接收的秘钥内容为【{}】长度为【{}】", password, password.length());
            return null;
        }
        //秘钥
        byte[] key = password.getBytes();
        //加载该类以支持 PKCS7Padding填充方式
        Security.addProvider(new BouncyCastleProvider());
        Cipher cipher;
        try {
            cipher = Cipher.getInstance(CIPHER_ALGORITHM);
            cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key, KEY_ALGORITHM));
            return Base64.encodeBase64String(cipher.doFinal(data.getBytes(StandardCharsets.UTF_8)));
        } catch (Exception e) {
            logger.error("加密3DES数据失败，源数据【{}】秘钥【{}】", data, password, e);
        }
        return null;
    }

    /**
     * 解密数据 -3DES
     *
     * @param data     源数据
     * @param password 秘钥 16位或者24位
     * @return
     */
    public static String decrypt(String data, String password) throws Exception {
        if (StringUtils.isBlank(data)) {
            logger.error("3DES解密，data为空");
            return null;
        }
        if (StringUtils.isBlank(password)) {
            logger.error("3DES解密，秘钥为空");
            return null;
        }
        if (password.length() != 16 && password.length() != 24) {
            logger.error("3DES解密，秘钥长度错误，长度需要16位或者24位，接收的秘钥内容为【{}】长度为【{}】", password, password.length());
            return null;
        }
        try {
            byte[] key = password.getBytes();
            //加载该类以支持 PKCS7Padding填充方式
            Security.addProvider(new BouncyCastleProvider());
            Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
            cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(key, KEY_ALGORITHM));
//			System.out.println(Base64.getDecoder().decode(data));
            return new String(cipher.doFinal(Base64.decodeBase64(data)), "utf-8");
        } catch (Exception e) {
            logger.error("解密3DES数据失败，源数据【{}】秘钥【{}】", data, password, e);
            throw e;
        }
    }

}